import org.apache.commons.io.DirectoryWalker;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by eahn on 01/11/13.
 */
public class DeleteFilesWalker extends DirectoryWalker<String> {

    private static Logger logger = LoggerFactory.getLogger(DeleteFilesWalker.class);

    protected String[] extensions;


    public DeleteFilesWalker(String... extensions) {
        this.extensions = extensions;
    }

    public List<String> start(File file) throws IOException {
        List<String> list = new ArrayList<String>();
        walk(file, list);
        return list;
    }


    protected void handleFile(File file, int depth, Collection<String> results) {

        for (String extension : extensions) {
            String filename = file.getName();

            if (filename.endsWith(extension)) {

                logger.debug("> File " + filename + " will be deleted");
                FileUtils.deleteQuietly(file);
                results.add(filename);

            }
        }

    }
}
